/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clipper.clipboard;

/**
 *
 * @author mahadevan
 */
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClipBored implements Runnable, ClipboardOwner {

    private static ClipBored clipBored;
    private static boolean hasOwnership = false;
    private static String[] clipboardTexts = new String[50];
    private static int textCount = 0;
    private static String lastClipboardText = new String("");
    private static Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    private static Transferable clipboardContents;
    private String stringToWrite;
    // Implement a singleton.Private constructor for that.

    private ClipBored() {
    }

    public static ClipBored getClipBored() {

        if (clipBored == null) {
            clipBored = new ClipBored();
        }

        return clipBored;
    }

    public boolean isClipboardEmpty() {

        if (clipboardContents == null) {

            return true;

        } else {

            return false;
        }

    }

    public boolean textAlreadyExists(String currentText) {
        boolean exists = false;

        for (int i = 0; i < textCount; i++) {
            if (clipboardTexts[i].equals(currentText)) {

                exists = true;
                return exists;
            }
        }

        return exists;
    }

    public String[] getClipboredTexts() {

        String currentClipboardText;

        clipboardContents = clipboard.getContents(null);

        try {

            currentClipboardText = (String) clipboardContents.getTransferData(DataFlavor.stringFlavor);

            if (!textAlreadyExists(currentClipboardText)) {

                clipboardTexts[textCount++] = currentClipboardText;
                lastClipboardText = currentClipboardText;

            }
        } catch (UnsupportedFlavorException ex) {

            return null;

        } catch (IOException ex) {

            return null;

        }

        return clipboardTexts;
    }

    public int getTextCount() {

        return textCount;

    }

    public void writeToClipBoard(String strClipText) {
        stringToWrite = strClipText;

        Thread thread = new Thread(this);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ClipBored.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void startWriting() {

        Transferable transferableText = new StringSelection(stringToWrite);
        clipboard.setContents(transferableText, this);

    }

    public void run() {

        startWriting();

    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
    }
}
